package com.xjl.javacore.code.reflexExample;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
 * 构建一个动态代理
 * @author xiaojinlu
 * @data 2020年1月17日16:41:19
 *
 * @return
 */
class MyDynamicProxy {
    public static void main (String[] args) {
        HelloImpl hello = new HelloImpl();
        MyInvocationHandler handler = new MyInvocationHandler(hello);
        // 构造代码实例
        Hello proxyHello = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), HelloImpl.class.getInterfaces(), handler);
        // 调用代理方法
        proxyHello.sayHello();
        }
}
    interface Hello {
        void sayHello();
        }
    class HelloImpl implements Hello {
        @Override
        public void sayHello() {
        System.out.println("Hello World");
        }
        }
        class MyInvocationHandler implements InvocationHandler {
        private Object target;

public MyInvocationHandler(Object target) {
                this.target = target;
            }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
                System.out.println("Invoking sayHello");
                Object result = method.invoke(target, args);
                return result;
            }
    }